<template>
	<view class="page">
		<up-cell-group :border="false">
			<up-cell
			    title="用户服务协议"
				isLink
				url="/pages/agreement/user"
			></up-cell>
			<up-cell
			    title="隐私协议"
				isLink
				url="/pages/agreement/privacy"
			></up-cell>
		</up-cell-group>
		<view class="logout" v-if="token && token !== ''">
			<up-button type="primary" shape="circle" color="#D19973" text="退出" @click="onLogout"></up-button>
		</view>
		<!-- <up-button type="primary" shape="circle" color="#D19973" text="测试支付" @click="testPay" customStyle="margin-top: 100rpx"></up-button> -->
	</view>
</template>

<script setup>
	import { ref } from 'vue'
	import { onShow } from '@dcloudio/uni-app'
	const token = ref(null)
	onShow(() => {
		token.value = uni.getStorageSync('token')
	})
	function onLogout() {
		uni.clearStorage()
		uni.showToast({
			icon: 'none',
			title: '已退出',
			duration: 3000,
			complete: () => {
				uni.reLaunch({
					url: '/pages/index/index'
				})
			}
		})
	}
	// 获取openid
	function testPay() {
		uni.login({
			success: (res) => {
				const { code } = res
				uni.request({
					url: `${getApp().globalData.baseUrl}/prod-api/wechat/weapp/code2session`,
					method: 'POST',
					data: {
						code
					},
					success: (sres) => {
						const { openid } = sres.data.session
						getPayData(openid)
					},
					fail: (error) => {
						console.log('error', error)
					}
				});
			}
		})
	}
	// 获取支付参数
	function getPayData(openid) {
		uni.request({
			url: `https://lumu.mynatapp.cc/wxpay/transactions/jsapi`,
			method: 'POST',
			data: {
				"appid": "wxe7972a210f1a3ea6",
				"description": "测试支付",
				"amount_total": 1,
				"openid": openid
			},
			success: (res) => {
				// weixin
				const { timeStamp, nonceStr, signType, paySign } = res.data.data
				wx.requestPayment({
					timeStamp,
					nonceStr,
					package: res.data.data.package,
					signType,
					paySign,
					success(res) {
						console.log('支付成功', res)
						uni.showToast({
							title: '支付成功'
						})
					},
					fail (res) {
						console.log('支付失败', res)
						uni.showToast({
							icon: 'error',
							title: '支付失败'
						})
					}
				})
			},
			fail: (error) => {
				console.log(error)
			}
		});
	}
</script>

<style lang="scss" scoped>
	.page {
		padding: 32rpx;
	}
	.logout {
		margin: 300rpx auto 0;
	}
</style>
